package main

import (
	"context"
	"fmt"
	clientv3 "go.etcd.io/etcd/client/v3"
	"time"
)

const (
	ali_host = "8.140.22.220"
	key      = "q1mi"
)

func watch() {
	cli, err := clientv3.New(clientv3.Config{
		Endpoints:   []string{ali_host + ":2379"},
		DialTimeout: 5 * time.Second,
	})
	if err != nil {
		fmt.Printf("连接失败, 错误信息:%v\n", err)
		return
	}
	fmt.Println("连接成功!")
	defer cli.Close()

	// channel 返回状态变更事件 <-chan WatchResponse
	rch := cli.Watch(context.Background(), key) //
	for wresp := range rch {
		for _, ev := range wresp.Events {
			fmt.Printf("状态类型: %s key:%s value:%s\n",
				ev.Type, ev.Kv.Key, ev.Kv.Value)
		}
	}
}

func main() {
	watch()
}
